# coding=utf-8

from flask import Blueprint, request, jsonify, json, make_response, redirect, g
from common.models.Food import Food, FoodType
from common.libs.user.UserLibs import UserLibs
from common.libs.Helper import ops_render
from common.libs.UrlManager import UrlManager
from application import app,db
from web.controllers.BaseControllerHelper import *
from .Helper import *

route_api_food = Blueprint( 'food_page',__name__ )

@route_api_food.route("/discount", methods=["GET"])
def discount():
    resp = gene_response(msg="OK")
    data = request.args
    shop_id = get_values_by_request(data, "shop_id")
    discount_food_list = Food.query.filter_by(is_discount=1, shop_id=shop_id).all()
    data_list = []
    if discount_food_list :
        for food in discount_food_list :
            temp_data = create_food_json(food, ["id", "name", "image", "discount", "present_price", "present_price", "original_price"])
            data_list.append(temp_data)
            resp = gene_response(code=200, msg="OK", data=data_list)
    else :
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)
    return jsonify( resp )

@route_api_food.route("/recommend", methods=["GET"])
def recommend():
    resp = gene_response(msg="OK")
    data = request.args
    shop_id = get_values_by_request(data, "shop_id")
    recommend_food_list = Food.query.filter_by(is_recommend=1, shop_id=shop_id).all()
    data_list = []
    if recommend_food_list:
        for food in recommend_food_list:
            temp_data = create_food_json(food, ["id", "name", "image"])
            data_list.append(temp_data)
            resp = gene_response(code=200, msg="OK", data=data_list)
    else:
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)
    return jsonify(resp)

@route_api_food.route("/get_by_type_id", methods=["GET"])
def get_by_type_id():
    resp = gene_response(msg="OK")
    data = request.args
    t_id = get_values_by_request(data, "t_id")
    shop_id = get_values_by_request(data, "shop_id")
    food_list = FoodType.query.filter(FoodType.id==t_id, Food.shop_id==shop_id).first().food_list
    data_list = {}
    data_list["type_id"] = t_id
    if food_list:
        temp_food_list = []
        for food in food_list:
            temp_data = create_food_json(food, ["id", "name", "image", "discount", "present_price", "present_price", "original_price"])
            temp_food_list.append(temp_data)
        data_list["food_list"] = temp_food_list
        resp = gene_response(code=200, msg="OK", data=data_list)
    else:
        data_list["food_list"] = []
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)
    return jsonify(resp)

@route_api_food.route("/get_one", methods=["GET"])
def get_one() :
    resp = gene_response(msg="OK")
    data = request.args
    data_list = []
    food_id = get_values_by_request(data, "food_id")
    food = Food.query.filter_by(id = food_id).first()
    if food:
        temp_data = create_food_json(food, ["id", "name", "image", "discount", "present_price", "present_price", "original_price"])
        data_list.append(temp_data)
        resp = gene_response(code=200, msg="OK", data=data_list)
    else :
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)
    return jsonify(resp)

